﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeSTrip.util;

namespace WeSTrip.controller
{
    public class TripController
    {
        public static string getCurrentTripId()
        {
            try
            {
                return DB.getValue("select IDENT_CURRENT('TRIP')");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getCurrentSliderId"));
                return "";
            }
        }
        public static string getTripId(int tripJoinId)
        {
            try
            {
                return DB.getValue("select TripID from TRIPJOIN where Id = "+tripJoinId+" ;");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getCurrentSliderId"));
                return "";
            }
        }
        public static string getJoinTripId(int tripId, int guestId)
        {
            try
            {
                return DB.getValue("select Id from TRIPJOIN where TripId = " + tripId + " and SysUserId= "+guestId+";");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getCurrentSliderId"));
                return "";
            }
        }
        public static bool CheckAvaiable(int tripId)
        {
            int order = Convert.ToInt32(DB.getValue("select COUNT (*) AS NumberOfOrders from TRIPJOIN where TripId= " + tripId + " and IsApproved = 1 and StatusId !=0;"));
            int slot = Convert.ToInt32(DB.getValue("select AvaiableSlot from TRIP where Id = " + tripId + " ;"));

            if (order >= slot)
                return false;
            else
                return true;

        }
        //check da join hay chua
        public static bool CheckJoinTrip(int tripId, int guestId)
        {
            int slot = Convert.ToInt32(DB.getValue("select COUNT (*) AS slot from TRIPJOIN where TripId= " + tripId + " and SysUserId=" + guestId + ";"));
            if (slot != 0 )
                return true;
            else
                return false;
        }
        public static bool CheckApprove(int tripId, int guestId)
        {
            string query = "select IsApproved from TRIPJOIN where TripId= " + tripId + " and SysUserId=" + guestId + ";";
            string approve = DB.getValue(query);
            if (approve == "True")
                return true;
            else
                return false;
        }
        public static bool CheckSTTTrip(int tripId)
        {
            string query = "select StatusId from TRIP where Id=" + tripId + ";";
            int status = Convert.ToInt32(DB.getValue(query));
            if (status == 1)
                return true;
            else
                return false;
        }
        public static bool CheckSTTJoinTrip(int tripId, int guestId)
        {
            string query = "select StatusId from TRIPJOIN where TripId=" + tripId + " and SysUserId=" + guestId + ";";
            int status = Convert.ToInt32(DB.getValue(query));
            if (status == 1)
                return true;
            else
                return false;
        }
        public static bool CheckSTTJoinTrip(int joinTripId)
        {
            string query = "select StatusId from TRIPJOIN where Id=" + joinTripId + ";";
            int status = Convert.ToInt32(DB.getValue(query));
            if (status == 1)
                return true;
            else
                return false;
        }
    }
}